﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Http;
using Polly;

namespace ConsoleApp
{
    public sealed class demo_retry
    {
        public static void demo()
        {
            //Pollydemo();

            //Palmerdemo();

            //Retry.RetryHelper.Instance.DefaultTryInterval = TimeSpan.FromSeconds(1);
            //Retry.RetryHelper.Instance.DefaultMaxTryCount = 3;
            //Retry.RetryHelper.Instance.Try(() =>
            //{
            //    HttpClient client = new HttpClient();
            //    try
            //    {
            //        var rs = client.GetStringAsync("http://localhost:59356/v2/api/banks/needtoquery.ashx").Result;
            //        //var rs = client.GetStringAsync("http://www.baidu.com").Result;
            //        Console.WriteLine(rs);
            //    }
            //    catch (Exception ex)
            //    {
            //        Console.WriteLine(ex.Message);
            //        throw new Exception(ex.Message);
            //    }
            //});

            int i = 0;
            HttpClient client = new HttpClient();
            do
            {
                try
                {
                    var rs = client.GetStringAsync("http://localhost:59356/v2/api/banks/needtoquery.ashx").Result;
                    //var rs = client.GetStringAsync("http://www.baidu.com").Result;
                    Console.WriteLine(rs);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message + "\r\n" + i++);
                    System.Threading.Thread.Sleep(1000);
                }
            }
            while (i < 3);
        }

        private static void Pollydemo()
        {
            var policy = Policy.Handle<AggregateException>().WaitAndRetry(5,
                retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)),
                (exception, timeSpan, context) =>
                {
                    Console.WriteLine(exception.Message + "\r\n" + timeSpan.TotalSeconds);
                });

            policy = Policy.Handle<AggregateException>().Retry(3,
                (exception, count, context) =>
                {
                    Console.WriteLine(exception.Message + "\r\n" + count);
                });

            var pxpolicy = Policy.Handle<Exception>().Retry(3);

            pxpolicy.Execute(() =>
            {
                HttpClient client = new HttpClient();
                try
                {
                    var rs = client.GetStringAsync("http://localhost:59356/v2/api/banks/needtoquery.ashx").Result;
                    //var rs = client.GetStringAsync("http://www.baidu.com").Result;
                    Console.WriteLine(rs);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception(ex.Message);
                }
            });
        }

        private static void Palmerdemo()
        {
            var counter = 0;

            Func<Palmer.RetryConditionHandle, bool> ab = (Palmer.RetryConditionHandle x) =>
            {
                return x.Occurences > 3;
            };

            Palmer.Retry.On<Exception>(ab).For(TimeSpan.FromSeconds(10)).With(context =>
            {
                try
                {
                    HttpClient client = new HttpClient();
                    var rs = client.GetStringAsync("http://localhost:59356/v2/api/banks/needtoquery.ashx").Result;
                    Console.WriteLine(rs);
                }
                catch (Exception ex)
                {
                    counter++;
                    Console.WriteLine(ex.Message);
                    throw new Exception(ex.Message);
                }
            });
        }
    }
}